package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.business.bean.ContactWorkerBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * @author zhangyu
 * @date 2019/2/18
 */
public class DialogContactWorker extends Dialog {
    private Disposable mDisposable;
    private BaseQuickAdapter<ContactWorkerBean, BaseViewHolder> mActionAdapter;

    public DialogContactWorker(Context context,
                               int repairId) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_contact_workers, null);
        final RecyclerView recyclerView = view.findViewById(R.id.recycler_view_worker);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setAdapter(mActionAdapter =
                new BaseQuickAdapter<ContactWorkerBean, BaseViewHolder>(R.layout.item_contact_worker,
                        null) {
                    @Override
                    protected void convert(BaseViewHolder helper, ContactWorkerBean item) {
                        String className = item.getName();
                        helper.setText(R.id.tv_name, className);
                        List<ContactWorkerBean.Worker> workers = item.getWorkers();
                        if ("过检".equals(className) || "竣检".equals(className)) {
                            helper.setTextColor(R.id.tv_workers,
                                    Color.parseColor("#8fa3b7"));
                            helper.setText(R.id.tv_workers, join(workers));
                        } else {
                            if (workers != null) {
                                StringBuilder name = new StringBuilder();
                                for (ContactWorkerBean.Worker worker : workers) {
                                    if (worker.isFinish()) {
                                        name.append(makeNameGreen(worker.getUserName())).append(",");
                                    } else {
                                        name.append(makeNameRed(worker.getUserName())).append(",");
                                    }
                                }
                                name = new StringBuilder(name.substring(0, name.length() - 1));
                                helper.setText(R.id.tv_workers,
                                        Html.fromHtml(name.toString()));
                            }
                        }

                    }
                });
        mActionAdapter.setEmptyView(R.layout.view_loading, (ViewGroup) recyclerView.getParent());
        mDisposable = RxRestClient.builder()
                .url(Url.GetOrderContactWorkers)
                .params("repairId", repairId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        List<ContactWorkerBean> list =
                                new ObjConverter<List<ContactWorkerBean>>().convert(response
                                        , new TypeReference<List<ContactWorkerBean>>() {
                                        });
                        mActionAdapter.setNewData(list);
                        mActionAdapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        mActionAdapter.setEmptyView(R.layout.view_search_empty,
                                (ViewGroup) recyclerView.getParent());
                    }
                })
                .build()
                .post();
        setContentView(view);
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }

    private String makeNameRed(String name) {
        name = "<font color='#FF0000'>" + name + "</font>";
        return name;
    }

    private String makeNameGreen(String name) {
        name = "<font color='#00ff00'>" + name + "</font>";
        return name;
    }

    private String join(List<ContactWorkerBean.Worker> list) {
        if (list != null) {
            StringBuilder name = new StringBuilder();
            for (ContactWorkerBean.Worker worker : list) {
                name.append(worker.getUserName()).append(",");
            }
            name = new StringBuilder(name.substring(0, name.length() - 1));
            return name.toString();
        } else {
            return "";
        }
    }
}
